﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using WebVideo.Areas.Admin.Models;
using System.Collections;

namespace WebVideo.Areas.Admin.Models
{
    public class Questions_EssayModel
    {
        public int Id { get; set; }

        //[Required(ErrorMessage = "Tên câu hỏi không được rỗng")]
        //[StringLength(50, ErrorMessage = "Tên phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        //public string Name { get; set; }

          [Required(ErrorMessage = "Nội dung không được để trống")]
         [StringLength(300, ErrorMessage = "Tên phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 10)]
        public string  Des { get; set; }


        public int IdVideo{get;set;}

        public DateTime Date { get; set; }

        // load
        public static IEnumerable<EntityQuestions_Essay> LayDuLieu(int IdEdit)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityQuestions_Essays
                        where p.IdVideo == IdEdit 
                        select p;

            return query.ToList();
        }

        public static void Insert(Questions_EssayModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityQuestions_Essay tnew = new EntityQuestions_Essay();
         //   tnew.Name = Mtnew.Name;
            tnew.Des = Mtnew.Des;
            tnew.IdVideo = Mtnew.IdVideo;
            tnew.Date = DateTime.Now ;

            db.EntityQuestions_Essays.InsertOnSubmit(tnew);
            db.SubmitChanges();

        }
        public static void InsertEmpty(int IdVideo)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityQuestions_Essay tnew = new EntityQuestions_Essay();
           
            tnew.Date = DateTime.Now;
            tnew.IdVideo = IdVideo;

            db.EntityQuestions_Essays.InsertOnSubmit(tnew);
            db.SubmitChanges();

        }


        public static Questions_EssayModel LayModel(int IdQVideo)
        {
            Questions_EssayModel Mtnew = new Questions_EssayModel();


            dbVideoDataContext db = new dbVideoDataContext();

            EntityQuestions_Essay tnew = db.EntityQuestions_Essays.Where(p => p.Id == IdQVideo).Single();

           // Mtnew.Name = tnew.Name;
            Mtnew.Id = tnew.Id;
            Mtnew.Des = tnew.Des;
            Mtnew.IdVideo  = (int) tnew.IdVideo;
            //Mtnew.Point = (int)tnew.Point;

            return Mtnew;

        }
        public static void Edit(Questions_EssayModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityQuestions_Essay tnew = db.EntityQuestions_Essays.Where(p => p.Id == Mtnew.Id).Single();
          //  tnew.Name = Mtnew.Name;
            tnew.Des = Mtnew.Des;
            tnew.IdVideo = Mtnew.IdVideo;
          //  tnew.Date = DateTime.Now;
            db.SubmitChanges();

        }

        public static void Delete(int Id)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityQuestions_Essay tnew = db.EntityQuestions_Essays.Where(p => p.Id == Id).Single();
            var query2 = db.EntityQuestions_Essays.Where(g => g.IdVideo == Id);// câu hỏi tự luận dành cho hình ảnh
            if (query2.ToList().Count > 0)
            {
                db.EntityQuestions_Essays.DeleteOnSubmit(tnew);
                db.SubmitChanges();
            }

        }

    }
}